2
2
.
.
8
8
.
.
1
1
B
B
u
u
s
s
i
i
n
n
e
e
s
s
s
s
L
L
o
o
g
g
i
i
c
c
-
-
I
I
n
n
s
s
i
i
d
d
e
e
C
C
o
o
n
n
t
t
r
r
o
o
l
l
l
l
e
e
r
r
I
I
n
n
f
f
o
o
[
[
G
G
]
]
We will start by making Controller that doesn't use Service.
Instead business logic is implemented inside the Controller itself.
In our example business logic is to return "Hello" String.
Such approach is not ideal because each Class should do one thing and one thing only.
But in this case Controller does two things
accepts incoming HTTP Requests
implements business logic
This causes following problems
Application becomes harder to maintain and test since everything is in one Class
Controller becomes big and unreadable by having all of the business logic placed inside its end points.
To change business logic we need to change the Controller potentially introducing bugs which might
affect its main purpose of routing HTTP Requests
affect other parts of business logic if Controller becomes corrupted
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: @Controller, @RequestMapping, Tomcat Server
hello()
MyController
http://localhost:8080/Hello
Browser
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_service_controller (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside package controllers)
MyController.java
package com.ivoronline.springboot_service_controller.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
//BUSINESS LOGIC
String result = "Hello from Controller";
//RETURN RESULT
return result;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Properties
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>